﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using SvnLib;

namespace ChkCommitMsg
{
	/// <summary>
	/// Put the following command into your pre-commit hook script(e.g. pre-commit.bat):
	/// <para>
	/// svnlook info %1 -t %2 | %REPOS%\hooks\PreCommitHook.exe
	/// </para>
	/// It will pass 'svnlook info' results via pipeline to this program. <br/>
	/// Note that %1 is the repository path, and %2 is transaction number. <br/><br/>
	/// 
	/// Written by Huan-Lin Tsai (http://huan-lin.blogspot.com).
	/// </summary>
	class Program
	{
		static int Main(string[] args)
		{
			SvnLookInfo lookInfo = SvnLookInfo.ParseStandardInput();

			if (!lookInfo.HasLogMessage())
			{
				// Write error message to console.
				using (StreamWriter sw = new StreamWriter(Console.OpenStandardError(), Encoding.Default))
				{
					string msg = ConfigurationManager.AppSettings["ErrorNoCommitMessage"];
					if (String.IsNullOrEmpty(msg))
					{
						msg = "Please input commit message!!";
					}
					sw.WriteLine(msg);
				}
				return 1;	// Tell Subversion to abort this commit.
			}

			return 0;	// OK!
		}
	}
}
